<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="流程名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter="handleFilter"
      />
      <el-select
        v-model="listQuery.status"
        placeholder="状态"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
      >
        新建流程
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        label="流程名称"
        prop="name"
        align="center"
        width="200"
      />
      <el-table-column
        label="流程描述"
        prop="description"
        align="center"
      />
      <el-table-column
        label="创建时间"
        prop="createTime"
        align="center"
        width="180"
      />
      <el-table-column
        label="状态"
        prop="status"
        align="center"
        width="100"
      >
        <template #default="{row}">
          <el-tag :type="row.status === 1 ? 'success' : 'info'">
            {{ row.status === 1 ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="250"
      >
        <template #default="{row}">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
          <el-button
            size="mini"
            type="warning"
            @click="handleDesign(row)"
          >
            设计
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="listQuery.page"
      v-model:limit="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'ProcessDesign',
  components: { Pagination },
  directives: { waves },
  setup() {
    const list = ref(null)
    const total = ref(0)
    const listLoading = ref(true)
    const listQuery = reactive({
      page: 1,
      limit: 20,
      name: undefined,
      status: undefined
    })

    const statusOptions = [
      { label: '启用', value: 1 },
      { label: '禁用', value: 0 }
    ]

    const getList = () => {
      listLoading.value = true
      // TODO: 调用API获取流程列表
      setTimeout(() => {
        list.value = []
        total.value = 0
        listLoading.value = false
      }, 1000)
    }

    const handleFilter = () => {
      listQuery.page = 1
      getList()
    }

    const handleCreate = () => {
      // TODO: 实现新建流程功能
    }

    const handleEdit = (row) => {
      // TODO: 实现编辑流程功能
    }

    const handleDelete = (row) => {
      // TODO: 实现删除流程功能
    }

    const handleDesign = (row) => {
      // TODO: 实现流程设计功能
    }

    onMounted(() => {
      getList()
    })

    return {
      list,
      total,
      listLoading,
      listQuery,
      statusOptions,
      handleFilter,
      handleCreate,
      handleEdit,
      handleDelete,
      handleDesign
    }
  }
}
</script>